<template>
  <div>
    <div class="title">
      <span v-if="!$route.query.id">写文章</span>
      <span v-else>编辑文章</span>
      <div>
        <el-button @click="fun">取消</el-button>
        <el-button
          type="primary"
          icon="el-icon-s-promotion"
          @click="fabuaa"
          v-if="!$route.query.id"
        >
          发布
        </el-button>
        <el-button
          type="primary"
          icon="el-icon-s-promotion"
          @click="bianji123"
          v-else
        >
          提交
        </el-button>
      </div>
    </div>
    <el-form
      :model="ruleForm"
      :rules="rules"
      ref="ruleForm"
      label-width="100px"
      class="demo-ruleForm"
    >
      <el-form-item label="标题" prop="title">
        <el-input v-model="ruleForm.title"></el-input>
      </el-form-item>
      <el-form-item label="内容" prop="region">
        <Toolbar
          style="border-bottom: 1px solid #ccc"
          :editor="editor"
          :defaultConfig="toolbarConfig"
          :mode="mode"
        />
        <Editor
          style="height: 400px; overflow-y: hidden"
          v-model="ruleForm.region"
          :defaultConfig="editorConfig"
          :mode="mode"
          @onCreated="onCreated"
        />
      </el-form-item>
      <el-form-item label="封面" prop="pic">
        <el-upload
          class="avatar-uploader"
          action="/api/admin/file/upload"
          :headers="uploadHeaders"
          :show-file-list="false"
          :on-success="handleAvatarSuccess"
          :before-upload="beforeAvatarUpload"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="" />
        </el-dialog>
      </el-form-item>
      <el-form-item label="摘要" prop="main">
        <el-input v-model="ruleForm.main"></el-input>
      </el-form-item>
      <el-form-item label="分类" prop="class">
        <el-select placeholder="请选择类别" v-model="ruleForm.class">
          <el-option
            v-for="item in ClassList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="标签" prop="label">
        <el-select placeholder="请选择标签" multiple v-model="ruleForm.label">
          <el-option
            v-for="item in LabelList"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import Vue from "vue";
import { Editor, Toolbar } from "@wangeditor/editor-for-vue";

export default Vue.extend({
  components: { Editor, Toolbar },
  data() {
    return {
      uploadHeaders: {
        Authorization: "Bearer " + localStorage.getItem("token"),
      },
      dialogVisible: false,
      dialogImageUrl: "",
      imageUrl: "",
      editor: null,
      html: "<p></p >",
      toolbarConfig: {},
      editorConfig: { placeholder: "请输入内容..." },
      mode: "default",
      ruleForm: {
        title: "",
        region: "",
        main: "",
        class: null,
        label: [],
        pic: "",
      },
      ruleForm1: {
        title: "",
        region: "",
        main: "",
        class: null,
        label: [],
        pic: "",
      },
      ClassList: [],
      LabelList: [],
      rules: {
        title: [{ required: true, message: "请输入活动名称", trigger: "blur" }],
        region: [{ required: true, message: "请输入内容", trigger: "blur" }],
        pic: [{ required: true, message: "请选择封面", trigger: "blur" }],
        main: [{ required: true, message: "请输入摘要", trigger: "blur" }],
        class: [{ required: true, message: "请选择类别", trigger: "change" }],
        label: [{ required: true, message: "请选择标签", trigger: "change" }],
      },
    };
  },
  methods: {
    onCreated(editor) {
      this.editor = Object.seal(editor);
    },
    fun() {
      this.ruleForm = JSON.parse(JSON.stringify(this.ruleForm1));
      this.$router.push("/houtai/wenzhang");
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.imageUrl = res.data.url;
      this.ruleForm.pic = res.data.url;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    async getLabel() {
      let res = await this.$axios.post("/admin/tag/select/list");
      this.LabelList = res.data.data;
    },
    async getClass() {
      let res = await this.$axios.post("/admin/category/select/list");
      this.ClassList = res.data.data;
    },
    async fabuaa() {
      await this.fabu();
      this.fun();
    },
    async fabu() {
      let res = await this.$axios.post("/admin/article/publish", {
        categoryId: this.ruleForm.class,
        content: this.ruleForm.region,
        description: this.ruleForm.main,
        id: null,
        tags: this.ruleForm.label,
        title: this.ruleForm.title,
        titleImage: this.ruleForm.pic,
      });
      console.log(this.ruleForm);
      console.log(res);
    },
    async bianji() {
      let res = await this.$axios.post("/admin/article/detail", {
        articleId: this.$route.query.id,
      });
      console.log(this.$route.query.id);
      console.log(res);
      console.log(res.data.data);
      this.ruleForm.region = res.data.data.content;
      this.ruleForm.main = res.data.data.description;
      this.ruleForm.pic = res.data.data.titleImage;
      this.imageUrl = res.data.data.titleImage;
      this.ruleForm.title = res.data.data.title;
      this.ruleForm.label = res.data.data.tagIds;
      this.ruleForm.class = res.data.data.categoryId;
    },
    async bianji123() {
      await this.bianji1();
      this.fun();
    },
    async bianji1() {
      let res = await this.$axios.post("/admin/article/update", {
        categoryId: this.ruleForm.class,
        content: this.ruleForm.region,
        description: this.ruleForm.main,
        id: this.$route.query.id,
        tags: this.ruleForm.label,
        title: this.ruleForm.title,
        titleImage: this.ruleForm.pic,
      });
      console.log(res);
    },
  },
  created() {
    this.getLabel();
    this.getClass();
    if (this.$route.query.id) {
      this.bianji();
    }
  },
  mounted() {
    setTimeout(() => {
      this.html = "<p></p >";
    }, 1500);
  },
  beforeDestroy() {
    const editor = this.editor;
    if (editor == null) return;
    editor.destroy();
  },
});
</script>

<style src="@wangeditor/editor/dist/css/style.css"></style>
<style>
.title {
  width: 100%;
  font-size: 25px;
  font-weight: bold;
  text-align: left;
  box-sizing: border-box;
  padding: 17px;
  display: flex;
  justify-content: space-between;
}
.demo-ruleForm {
  text-align: left;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>